/*============================================================================
 * Copyright 2009 VMware Inc.
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
 * use this file except in compliance with the License. You may obtain a copy 
 * of the License at: http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 * License for the specific language governing permissions and limitations 
 * under the License.
 ============================================================================*/

package com.vmware.vcloud;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;

import com.vmware.vcloud.data.VCloudConstants;
import com.vmware.vcloud.rest.RestClient;

/**
 * @author Steve Jin (sjin@vmware.com)
 */

public class Catalog extends VCloudObject
{
	List<CatalogItem> catalogItems = new ArrayList<CatalogItem>();
	
	public Catalog(RestClient rc, String urlPath) throws MalformedURLException
	{
		super(rc, urlPath);
	}
	
	protected void parse() throws MalformedURLException
	{
		List<?> cis = root.element(VCloudConstants.ELEM_CATALOGITEMS)
			.elements(VCloudConstants.ELEM_CATALOGITEM);
		
		for(int i=0; i<cis.size(); i++)
		{
			Element ci = (Element) cis.get(i);
			String href = ci.attributeValue("href");
			String name = ci.attributeValue("name");
			catalogItems.add(new CatalogItem(rc, href, name));
		}
	}
	
	public CatalogItem[] getCatalogItems() throws IOException
	{
		loadContentIfNotYet();
		
		CatalogItem[] cis = new CatalogItem[catalogItems.size()];
		catalogItems.toArray(cis);
		return cis;
	}
}
